package edu.cueb.wmis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.cueb.wmis.mapper.*;
import edu.cueb.wmis.pojo.*;
import edu.cueb.wmis.service.IGoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements IGoodService {

    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private ShelfMapper shelfMapper;
    @Autowired
    private IncomebillitemMapper incomebillitemMapper;
    @Autowired
    private IncomebillMapper incomebillMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private HouseShelfMapper houseShelfMapper;
    @Override
    //入库存入tb_good
    @Transactional
    public boolean addGood(Incomebillitem incomebillitem) {
        Good good = new Good();
        good.setName(incomebillitem.getName());
        good.setSpec(incomebillitem.getSpec());
        good.setUnit(incomebillitem.getUnit());
        good.setPrice(incomebillitem.getPrice());
        good.setProductionTime(incomebillitem.getProductionTime());
        good.setShelfLife(incomebillitem.getShelfLife());
        good.setStoreNumber(incomebillitem.getInNumber());
        good.setShelfId(incomebillitem.getShelfId());
        try {
            if(goodMapper.insert(good)==1){
                List<Map<String, Object>> list = goodMapper.selectMaps(new QueryWrapper<Good>().select("max(id)"));
                Integer goodId = Integer.parseInt(list.get(0).get("max(id)").toString());
                List<Map<String, Object>> list2 = incomebillitemMapper.selectMaps(new QueryWrapper<Incomebillitem>().select("max(id)"));
                Integer itemId = Integer.parseInt(list2.get(0).get("max(id)").toString());
                incomebillitemMapper.setItemGoodId(goodId,itemId);
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public RespPageBean getAllGood(Integer currentPage, Integer size, Good good) {
        Page<Good> page = new Page<>(currentPage , size);
        IPage<Good> goodByPage = goodMapper.getGoodByPage(page, good);

        RespPageBean respPageBean = new RespPageBean(goodByPage.getTotal(),goodByPage.getRecords());

        return respPageBean;
    }

    @Override
    public RespBean getGoodStoreNumberByName(String gname) {

        try {
            List<Good> goodsByGname = goodMapper.selectList(new QueryWrapper<Good>().like("name", gname));

            Integer count = 0;
            for(Good good : goodsByGname){
                count+=good.getStoreNumber();
            }
            return RespBean.success("成功",count);
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("失败");
        }
    }

    @Override
    public List<Good> getGoodsByHouseId(Integer hid) {
        List<Shelf> shelves = shelfMapper.getShelfsByHouseId(hid);

        List<Integer> shelfIds = new ArrayList<>();
        for(Shelf shelf : shelves){
            shelfIds.add(shelf.getId());
        }
        List<Good> goods = goodMapper.getGoodsByShelfIds(shelfIds);
        List<Good> goodRes = new LinkedList<>();
        for(Good good : goods){

            goodRes.add(goodMapper.getGoodInfo(good.getId()));
        }

        return goodRes;

    }

    @Override
    public List<Good> getGoodsByShelfId(Integer sid) {
        return goodMapper.selectList(new QueryWrapper<Good>().eq("shelfId",sid));
    }

    @Override
    public List<Good> getGoodZero() {
        //return goodMapper.getGoodZero();
        List<Good> goods = goodMapper.getGoodZero();
        List<Good> temp = new LinkedList<>();
        for(Good good : goods){
            temp.add(goodMapper.getGoodInfo(good.getId()));
        }
        return temp;
    }

    @Override
    public List<Shelf> getShelfZero() {
        List<Integer> shelfIds = goodMapper.getShelfZero();
        List<Shelf> shelves = new LinkedList<>();
        String hname = "";
        for(Integer shelfId : shelfIds){
            HouseShelf hs = houseShelfMapper.selectOne(new QueryWrapper<HouseShelf>().eq("sid", shelfId));
            hname = houseMapper.selectById(hs.getHid()).getName();

            Shelf shelf = new Shelf();
            shelf.setId(shelfId);
            shelf.setName(hname+shelfMapper.selectById(shelfId).getName());
            shelves.add(shelf);
        }
        return shelves;
    }

    @Override
    public  List<Good> getNullGoods() {
        List<Good> goods = goodMapper.getNullGoods();
        for(Good good : goods){
            Integer shelfId = good.getShelfId();
            HouseShelf hs = houseShelfMapper.selectOne(new QueryWrapper<HouseShelf>().eq("sid", shelfId));
            good.setHname(houseMapper.selectById(hs.getHid()).getName());
            good.setSname(shelfMapper.selectById(shelfId).getName());
            Incomebill incomeBill = incomebillMapper.getIncomeBillByGoodId(good.getId());
            good.setIncomeTime(incomeBill.getInputTime());
            good.setAdminName(adminMapper.getNameById(incomeBill.getInputUserId()));
        }
        return goods;
    }
}
